package net.javaw.callplatform;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class MyContacts extends ListActivity {

	/** 获取库Phon表字段 **/
	private static final String[] PHONES_PROJECTION = new String[] {
			Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID, Phone.CONTACT_ID };

	/** 联系人显示名称 **/
	private static final int PHONES_DISPLAY_NAME_INDEX = 0;

	/** 电话号码 **/
	private static final int PHONES_NUMBER_INDEX = 1;

	/** 联系人名称 **/
	private ArrayList<String> mContactsName = new ArrayList<String>();

	/** 联系人头像 **/
	private ArrayList<String> mContactsNumber = new ArrayList<String>();

	ListView mListView = null;
	MyListAdapter myAdapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mListView = this.getListView();
		
		/** 得到手机通讯录联系人信息 **/
		getPhoneContacts();

		myAdapter = new MyListAdapter(mContactsName,mContactsNumber);
		mListView.setAdapter(myAdapter);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,int position, long id) {
				
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
		    	intent.putExtra("name", mContactsName.get(position));
		    	intent.putExtra("number",mContactsNumber.get(position));
		    	intent.putExtras(bundle);
				setResult(20, intent);//设置返回数据
				finish();//关闭Activity(关闭窗口)
			}
		});

		
	}

	/** 得到手机通讯录联系人信息 **/
	@SuppressLint("NewApi")
	private void getPhoneContacts() {
		ContentResolver resolver = this.getContentResolver();

		// 获取手机联系人
		Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,
				PHONES_PROJECTION, null, null, null);

		if (phoneCursor != null) {
			while (phoneCursor.moveToNext()) {

				// 得到手机号码
				String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
				// 当手机号码为空的或者为空字段 跳过当前循环
				if (TextUtils.isEmpty(phoneNumber))
					continue;

				// 得到联系人名称
				String contactName = phoneCursor
						.getString(PHONES_DISPLAY_NAME_INDEX);

				mContactsName.add(contactName);
				mContactsNumber.add(phoneNumber);
			}

			phoneCursor.close();
		}
	}

	class MyListAdapter extends BaseAdapter {
		View[] itemViews;  
		  
        public MyListAdapter(ArrayList<String> mContactsName, ArrayList<String> mContactsNumber) {  
            itemViews = new View[mContactsName.size()];  
  
            for (int i = 0; i < itemViews.length; i++) {  
                itemViews[i] = makeItemView(mContactsName.get(i), mContactsNumber.get(i));  
            }  
        }  
  
        public int getCount() {  
            return itemViews.length;  
        }  
  
        public View getItem(int position) {  
            return itemViews[position];  
        }  
  
        public long getItemId(int position) {  
            return position;  
        }  
  
        private View makeItemView(String mName, String mNumber) {  
        	
            LayoutInflater inflater = (LayoutInflater) MyContacts.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  
            // 使用View的对象itemView与R.layout.item关联   
            View itemView = inflater.inflate(R.layout.mycontacts, null);  
  
            // 通过findViewById()方法实例R.layout.item内各组件   
            TextView name = (TextView) itemView.findViewById(R.id.contact_name);  
            name.setText(mName);  
            TextView number = (TextView) itemView.findViewById(R.id.contact_number);  
            number.setText(mNumber);  
              
            return itemView;  
        }  
  
		public View getView(int position, View convertView, ViewGroup parent) {
			View itemView = null;
			
			if (convertView == null)
			{
				itemView = itemViews[position];
			}else
			{
				itemView = convertView;
				
				// 通过findViewById()方法实例R.layout.item内各组件   
	            TextView name = (TextView) itemView.findViewById(R.id.contact_name);  
	            name.setText(mContactsName.get(position));  
	            TextView number = (TextView) itemView.findViewById(R.id.contact_number);  
	            number.setText(mContactsNumber.get(position));  
			}
			
            return itemView;  
		}

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有\n\n免责声明\n\n河南卓辉通信版权所有\n\n作者：史守阳\n\n仅限河南卓辉通信内部员工使用", Toast.LENGTH_LONG).show();
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) 
		{
			// 启动Main Activity
			Intent intent = new Intent(MyContacts.this, Notifications.class);
			startActivity(intent);
			// 结束该Activity
			finish();
		}else if(keyCode == KeyEvent.KEYCODE_MENU) 
		{
			Toast.makeText(getApplicationContext(), "待扩展。。。\n\n河南卓辉通信技术有限公司版权所有\n\n免责声明\n\n河南卓辉通信版权所有\n\n作者：史守阳\n\n仅限河南卓辉通信内部员工使用", Toast.LENGTH_LONG).show();
		}
		return true;
	}
}